package com.cn.theory;

import java.lang.reflect.Field;

public class AnnotationProcessor {
    public static void processAnnotations(Object object){
        //获取类对象
        Class<?> clazz = object.getClass();
        //获取所有字段
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if(field.isAnnotationPresent(AutoAssignOne.class)){ // 判断是否有@AutoAssignOne注解
                field.setAccessible(true);
                try{
                    if(field.getType()==int.class){// 仅处理 int 类型字段
                        field.set(object,1);//赋值为1
                    }
                }catch (IllegalAccessException e){
                    e.printStackTrace();
                }

            }
        }

    }

    public static void main(String[] args) {
        TestClass testClass = new TestClass();
        //处理注解并赋值
        System.out.println("Before processing: "+testClass);
        processAnnotations(testClass);
        System.out.println("After processing: " + testClass);

    }
}
